//
//  LSNetWorkTools.swift
//  新浪微博
//
//  Created by 梁森 on 16/6/26.
//  Copyright © 2016年 梁森. All rights reserved.
//

import UIKit
import AFNetworking
enum MethodType:String{
    case GET = "GET"
    case POST = "POST"
}
class LSNetWorkTools: AFHTTPSessionManager {
    static let shareTools : LSNetWorkTools = {
       let tool = LSNetWorkTools()
        tool.responseSerializer.acceptableContentTypes?.insert("text/plain")
        return tool
    }()
    
    func request(method:MethodType,urlString:String,params:AnyObject?,success:(res:AnyObject?)->(),failure:(err:NSError)->()){
        if method == MethodType.GET{
            self.GET(urlString, parameters: params, progress: nil, success: { (_, response) -> Void in
                success(res: response)
                }, failure: { (_, error) -> Void in
                    failure(err: error)
            })
        }else{
            self.POST(urlString, parameters: params, progress: nil, success: { (_, response) -> Void in
                success(res: response)
                }, failure: { (_, error) -> Void in
                    failure(err: error)
            })
        }
    }
    
}
extension LSNetWorkTools{
   
    func oAuthloadUserAccount(code:String,success:(response:AnyObject?)->(),failure:(error:NSError)->()){
        let urlString = "https://api.weibo.com/oauth2/access_token"
        let params = [
            "client_id":APPKEY,
            "client_secret":APPSECRET,
            "grant_type":"authorization_code",
            "code":code,
            "redirect_uri":REDIRECT_URI
        ]
        
        self.request(MethodType.POST, urlString: urlString, params: params, success: success, failure: failure)
    }
    
    func oAuthLoadUserInfo(oAuthModel:LSoAuthModel,success:(response:AnyObject?)->(),failure:(error:NSError)->()){
    let url = "https://api.weibo.com/2/users/show.json"
        let params = [
            "access_token":oAuthModel.access_token!,
             "uid":oAuthModel.uid!
        ]
        self.request(MethodType.GET, urlString: url, params: params, success: success, failure: failure)
    }
}